package com.guyaning.deyson_ec.ec.main.sort.content;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/28.
 */

public class SectionDataConverter {

    public List<SectionBean> convert(String json) {

        List<SectionBean> dataList = new ArrayList<>();

        JSONArray jsonArray = JSONArray.parseObject(json).getJSONArray("data");

        int size = jsonArray.size();

        for (int i = 0; i < size; i++) {
            JSONObject data = jsonArray.getJSONObject(i);

            int id = data.getInteger("id");

            String section = data.getString("section");

            //添加title
            SectionBean sectionBean = new SectionBean(true, section);

            sectionBean.setmId(id);

            sectionBean.setmIsMore(true);

            dataList.add(sectionBean);

            JSONArray goodsArray = data.getJSONArray("goods");

            int goodsSize = goodsArray.size();

            //商品具体内容的循环
            for (int j = 0; j < goodsSize; j++) {

                JSONObject contentItem = goodsArray.getJSONObject(j);
                int goodsId = contentItem.getInteger("goods_id");
                String goodsName = contentItem.getString("goods_name");
                String goodsThumb = contentItem.getString("goods_thumb");

                //转换填充对象
                SectionContentItemEntry contentItemEntry = new SectionContentItemEntry();
                contentItemEntry.setmGoodsId(goodsId);
                contentItemEntry.setmGoodsName(goodsName);
                contentItemEntry.setmGoodsThumb(goodsThumb);

                //将商品内容添加到数据集合中
                dataList.add(new SectionBean(contentItemEntry));
            }


        }

        return dataList;
    }
}
